<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Working with Third-Party Code - Special Topics - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w1650" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w1650-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w1650-collapse" class="collapse navbar-collapse"><ul id="w1651" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w1652" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-1634" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-1634" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-1635" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-1635" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-1636" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-1636" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item" href="#navigation-1637" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-1637" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-1638" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-1638" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-1639" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-1639" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-1640" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-1640" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-1641" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-1641" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-1642" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-1642" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-1643" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-1643" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item" href="#navigation-1644" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-1644" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-1645" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-1645" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-1646" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-1646" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item active" href="#navigation-1647" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-1647" class="submenu panel-collapse collapse in"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item active" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-1648" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-1648" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-1649" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-1649" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Working with Third-Party Code <span id="working-with-third-party-code"></span><a href="#working-with-third-party-code" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#using-libs-in-yii">Using Third-Party Libraries in Yii</a></li>
<li><a href="#using-yii-in-others">Using Yii in Third-Party Systems</a></li>
<li><a href="#using-both-yii2-yii1">Using Yii 2 with Yii 1</a></li></ol></div>
<p>From time to time, you may need to use some third-party code in your Yii applications. Or you may want to
use Yii as a library in some third-party systems. In this section, we will show how to achieve these goals.</p>
<h2>Using Third-Party Libraries in Yii  <span id="using-libs-in-yii"></span><a href="#using-libs-in-yii" class="hashlink">&para;</a></h2><p>To use a third-party library in a Yii application, you mainly need to make sure the classes in the library
are properly included or can be autoloaded.</p>
<h3>Using Composer Packages  <span id="using-composer-packages"></span><a href="#using-composer-packages" class="hashlink">&para;</a></h3><p>Many third-party libraries are released in terms of <a href="https://getcomposer.org/">Composer</a> packages.
You can install such libraries by taking the following two simple steps:</p>
<ol>
<li>modify the <code>composer.json</code> file of your application and specify which Composer packages you want to install.</li>
<li>run <code>composer install</code> to install the specified packages.</li>
</ol>
<p>The classes in the installed Composer packages can be autoloaded using the Composer autoloader. Make sure
the <a href="guide-structure-entry-scripts.html">entry script</a> of your application contains the following lines to install
the Composer autoloader:</p>
<pre><code class="hljs php language-php"><span class="hljs-comment">// install Composer autoloader</span>
<span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../vendor/autoload.php'</span>);

<span class="hljs-comment">// include Yii class file</span>
<span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../vendor/yiisoft/yii2/Yii.php'</span>);
</code></pre>
<h3>Using Downloaded Libraries  <span id="using-downloaded-libs"></span><a href="#using-downloaded-libs" class="hashlink">&para;</a></h3><p>If a library is not released as a Composer package, you should follow its installation instructions to install it.
In most cases, you will need to download a release file manually and unpack it in the <code>BasePath/vendor</code> directory,
where <code>BasePath</code> represents the <a href="guide-structure-applications.html#basePath">base path</a> of your application.</p>
<p>If a library carries its own class autoloader, you may install it in the <a href="guide-structure-entry-scripts.html">entry script</a>
of your application. It is recommended the installation is done before you include the <code>Yii.php</code> file so that
the Yii class autoloader can take precedence in autoloading classes.</p>
<p>If a library does not provide a class autoloader, but its class naming follows <a href="http://www.php-fig.org/psr/psr-4/">PSR-4</a>,
you may use the Yii class autoloader to autoload the classes. All you need to do is just to declare a
<a href="guide-concept-aliases.html#defining-aliases">root alias</a> for each root namespace used in its classes. For example,
assume you have installed a library in the directory <code>vendor/foo/bar</code>, and the library classes are under
the <code>xyz</code> root namespace. You can include the following code in your application configuration:</p>
<pre><code class="hljs php language-php">[
    <span class="hljs-string">'aliases'</span> =&gt; [
        <span class="hljs-string">'@xyz'</span> =&gt; <span class="hljs-string">'@vendor/foo/bar'</span>,
    ],
]
</code></pre>
<p>If neither of the above is the case, it is likely that the library relies on PHP include path configuration to
correctly locate and include class files. Simply follow its instruction on how to configure the PHP include path.</p>
<p>In the worst case when the library requires explicitly including every class file, you can use the following method
to include the classes on demand:</p>
<ul>
<li>Identify which classes the library contains.</li>
<li>List the classes and the corresponding file paths in <code>Yii::$classMap</code> in the <a href="guide-structure-entry-scripts.html">entry script</a>
of the application. For example,<pre><code class="hljs php language-php">Yii::<span class="hljs-variable">$classMap</span>[<span class="hljs-string">'Class1'</span>] = <span class="hljs-string">'path/to/Class1.php'</span>;
Yii::<span class="hljs-variable">$classMap</span>[<span class="hljs-string">'Class2'</span>] = <span class="hljs-string">'path/to/Class2.php'</span>;
</code></pre>
</li>
</ul>
<h2>Using Yii in Third-Party Systems  <span id="using-yii-in-others"></span><a href="#using-yii-in-others" class="hashlink">&para;</a></h2><p>Because Yii provides many excellent features, sometimes you may want to use some of its features to support
developing or enhancing 3rd-party systems, such as WordPress, Joomla, or applications developed using other PHP
frameworks. For example, you may want to use the <a href="./yii-helpers-arrayhelper.html">yii\helpers\ArrayHelper</a> class or use the
<a href="guide-db-active-record.html">Active Record</a> feature in a third-party system. To achieve this goal, you mainly need to
take two steps: install Yii, and bootstrap Yii.</p>
<p>If the third-party system uses Composer to manage its dependencies, you can simply run the following commands
to install Yii:</p>
<pre><code class="hljs php">composer <span class="hljs-keyword">global</span> <span class="hljs-keyword">require</span> <span class="hljs-string">"fxp/composer-asset-plugin:^1.2.0"</span>
composer <span class="hljs-keyword">require</span> yiisoft/yii2
composer install
</code></pre>
<p>The first command installs the <a href="https://github.com/francoispluchino/composer-asset-plugin/">composer asset plugin</a>
which allows managing bower and npm package dependencies through Composer. Even if you only want to use the database
layer or other non-asset related features of Yii, this is required to install the Yii composer package.</p>
<p>If you want to use the <a href="guide-structure-assets.html">Asset publishing feature of Yii</a> you should also add the following configuration
to the <code>extra</code> section in your <code>composer.json</code>:</p>
<pre><code class="hljs json language-json">{
    ...
    "<span class="hljs-attribute">extra</span>": <span class="hljs-value">{
        "<span class="hljs-attribute">asset-installer-paths</span>": <span class="hljs-value">{
            "<span class="hljs-attribute">npm-asset-library</span>": <span class="hljs-value"><span class="hljs-string">"vendor/npm"</span></span>,
            "<span class="hljs-attribute">bower-asset-library</span>": <span class="hljs-value"><span class="hljs-string">"vendor/bower"</span>
        </span>}
    </span>}
</span>}
</code></pre>
<p>See also the general <a href="guide-start-installation.html#installing-via-composer">section about installing Yii</a> for more information
on Composer and solution to possible issues popping up during the installation.</p>
<p>Otherwise, you can <a href="http://www.yiiframework.com/download/">download</a> the Yii release file and unpack it in
the <code>BasePath/vendor</code> directory.</p>
<p>Next, you should modify the entry script of the 3rd-party system by including the following code at the beginning:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../vendor/yiisoft/yii2/Yii.php'</span>);

<span class="hljs-variable">$yiiConfig</span> = <span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../config/yii/web.php'</span>);
<span class="hljs-keyword">new</span> yii\web\Application(<span class="hljs-variable">$yiiConfig</span>); <span class="hljs-comment">// Do NOT call run() here</span>
</code></pre>
<p>As you can see, the code above is very similar to that in the <a href="guide-structure-entry-scripts.html">entry script</a> of
a typical Yii application. The only difference is that after the application instance is created, the <code>run()</code> method
is not called. This is because by calling <code>run()</code>, Yii will take over the control of the request handling workflow
which is not needed in this case and already handled by the existing application.</p>
<p>Like in a Yii application, you should configure the application instance based on the environment running
the third-party system. For example, to use the <a href="guide-db-active-record.html">Active Record</a> feature, you need to configure
the <code>db</code> <a href="guide-structure-application-components.html">application component</a> with the DB connection setting used by the third-party system.</p>
<p>Now you can use most features provided by Yii. For example, you can create Active Record classes and use them
to work with databases.</p>
<h2>Using Yii 2 with Yii 1  <span id="using-both-yii2-yii1"></span><a href="#using-both-yii2-yii1" class="hashlink">&para;</a></h2><p>If you were using Yii 1 previously, it is likely you have a running Yii 1 application. Instead of rewriting
the whole application in Yii 2, you may just want to enhance it using some of the features only available in Yii 2.
This can be achieved as described below.</p>
<blockquote class="note"><p><strong>Note: </strong>Yii 2 requires PHP 5.4 or above. You should make sure that both your server and the existing application
support this.</p>
</blockquote>
<p>First, install Yii 2 in your existing application by following the instructions given in the <a href="#using-yii-in-others">last subsection</a>.</p>
<p>Second, modify the entry script of the application as follows,</p>
<pre><code class="hljs php language-php"><span class="hljs-comment">// include the customized Yii class described below</span>
<span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../components/Yii.php'</span>);

<span class="hljs-comment">// configuration for Yii 2 application</span>
<span class="hljs-variable">$yii2Config</span> = <span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../config/yii2/web.php'</span>);
<span class="hljs-keyword">new</span> yii\web\Application(<span class="hljs-variable">$yii2Config</span>); <span class="hljs-comment">// Do NOT call run(), yii2 app is only used as service locator</span>

<span class="hljs-comment">// configuration for Yii 1 application</span>
<span class="hljs-variable">$yii1Config</span> = <span class="hljs-keyword">require</span>(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../config/yii1/main.php'</span>);
Yii::createWebApplication(<span class="hljs-variable">$yii1Config</span>)-&gt;run();
</code></pre>
<p>Because both Yii 1 and Yii 2 have the <code>Yii</code> class, you should create a customized version to combine them.
The above code includes the customized <code>Yii</code> class file, which can be created as follows.</p>
<pre><code class="hljs php language-php"><span class="hljs-variable">$yii2path</span> = <span class="hljs-string">'/path/to/yii2'</span>;
<span class="hljs-keyword">require</span>(<span class="hljs-variable">$yii2path</span> . <span class="hljs-string">'/BaseYii.php'</span>); <span class="hljs-comment">// Yii 2.x</span>

<span class="hljs-variable">$yii1path</span> = <span class="hljs-string">'/path/to/yii1'</span>;
<span class="hljs-keyword">require</span>(<span class="hljs-variable">$yii1path</span> . <span class="hljs-string">'/YiiBase.php'</span>); <span class="hljs-comment">// Yii 1.x</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Yii</span> <span class="hljs-keyword">extends</span> \<span class="hljs-title">yii</span>\<span class="hljs-title">BaseYii</span>
</span>{
    <span class="hljs-comment">// copy-paste the code from YiiBase (1.x) here</span>
}

Yii::<span class="hljs-variable">$classMap</span> = <span class="hljs-keyword">include</span>(<span class="hljs-variable">$yii2path</span> . <span class="hljs-string">'/classes.php'</span>);
<span class="hljs-comment">// register Yii 2 autoloader via Yii 1</span>
Yii::registerAutoloader([<span class="hljs-string">'yii\BaseYii'</span>, <span class="hljs-string">'autoload'</span>]);
<span class="hljs-comment">// create the dependency injection container</span>
Yii::<span class="hljs-variable">$container</span> = <span class="hljs-keyword">new</span> yii\di\Container;
</code></pre>
<p>That's all! Now in any part of your code, you can use <code>Yii::$app</code> to access the Yii 2 application instance, while
<code>Yii::app()</code> will give you the Yii 1 application instance:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">echo</span> get_class(Yii::app()); <span class="hljs-comment">// outputs 'CWebApplication'</span>
<span class="hljs-keyword">echo</span> get_class(Yii::<span class="hljs-variable">$app</span>);  <span class="hljs-comment">// outputs 'yii\web\Application'</span>
</code></pre>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:31 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
